package com.linkhub.oms.data.sync.api.wms.domain.request;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ReflectUtil;
import com.linkhub.oms.common.util.OffSetDateTimeUtil;
import lombok.Data;
import lombok.SneakyThrows;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: BaseRequest
 * @Author: echo
 * @Date: 2024/5/5 20:02
 * @Version: 1.0
 * @see {https://vwms-wiki.yuque.com/org-wiki-vwms-wiki-bu7gws/dzkqs6/llxpn7nlpf1yuwda}
 */
@Data
public class WmsBaseURLRequest implements Serializable {

    private String method;

    private String timestamp;

    private String format = "json";

    private String app_key;

    private String v = "2.0";

    private String sign_method = "md5";

    private String customerId;


//    public WmsBaseURLRequest(String method, String timestamp, String app_key, String customerId) {
//        this.method = method;
//        this.timestamp = timestamp;
//        this.app_key = app_key;
//        this.customerId = customerId;
//    }


    @SneakyThrows
    public WmsBaseURLRequest( String method, String app_key, String customerId ) {
        this.method = method;
        this.app_key = app_key;
        this.customerId = customerId;
        this.timestamp =
                LocalDateTimeUtil.format(OffSetDateTimeUtil.nowWithBeijingZone(), DatePattern.NORM_DATETIME_FORMATTER);
    }

//    public static WmsBaseURLRequest createSimpleRequest( String method, String appKey, String customerId, String NotificaitonURL ) {
//        return new WmsBaseURLRequest(method, appKey, customerId, NotificaitonURL);
//    }

    public static WmsBaseURLRequest createSimpleRequest( String method, String appKey, String customerId ) {
        return new WmsBaseURLRequest(method, appKey, customerId);
    }

    public String sortAndJoinFields() {

        return Stream.of(ReflectUtil.getFields(this.getClass()))
                .sorted(Comparator.comparing(Field::getName))
                .map(field -> String.format("%s%s", field.getName(), ReflectUtil.getFieldValue(this, field)))
                .collect(Collectors.joining());

    }


}
